package cn.wolfcode.luowowo.mrgsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据持久化定时任务
 * 把redis中指定的数据定时更新到mysql中
 */
//@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    /**
     *                                                     年
     *  Seconds Minutes Hours DayofMonth Month DayofWeek Year
     *  秒       分       时       号       月       周几
     *  Seconds Minutes Hours DayofMonth Month DayofWeek (spring支持该类)
     *
     *  0       0         2      1          *       ?       *   表示在每月的1日的凌晨2点调整任务
     *
     *  0       15       10      ?          *    MON-FRI(周一到周五)        表示周一到周五每天上午10:15执行作业
     */

    //定时任务注解
    //用于指定定时执行规则(时间计划)
    //cron : 时间计划表达式
    //@Scheduled(cron = "0/10 * * * * ? ")
    public void doWork(){

        System.out.println("----------vo-持久化-start------------");

        //1.从redis中获取所有vo对象             根据前缀拿到所有vo对象
        List<StrategyStatisVO> list = strategyStatisVOService.getVosByPattern(RedisKeys.STRATEGY_STATIS_VO.join("*"));

        //2.将vo对象update到mysql数据库中
        StrategyStatisPersistenceVO pvo = null;

        for (StrategyStatisVO vo : list) {
            pvo = new StrategyStatisPersistenceVO();
            BeanUtil.copyProperties(pvo,vo);

            strategyDetailService.updateStatisVo(pvo);
        }

        System.out.println("----------vo-持久化-end------------");

    }
}
